
package com.market.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.market.bean.Message;
import com.market.web.form.MsgSearchForm;

public class MessageServiceImpl extends BaseServiceImpl implements IMessageService
{
    private DetachedCriteria setDetachCri(MsgSearchForm msf)
    {
        DetachedCriteria detachCri = DetachedCriteria.forClass(Message.class);
        if (!"all".equals(msf.getCategory()) && !"".equals(msf.getCategory()))
        {
            detachCri.add(Restrictions.eq("category", msf.getCategory()));
        }
        if (!"all".equals(msf.getType()) && !"".equals(msf.getType()))
        {
            detachCri.add(Restrictions.eq("type", msf.getType()));
        }
        if (!"".equals(msf.getSubject()))
        {
            detachCri.add(Restrictions.like("subject", msf.getSubject(), MatchMode.ANYWHERE));
        }
        if ("hot".equals(msf.getOrderBy()))
        {
            detachCri.addOrder(Order.desc(msf.getOrderBy()));
        }
        else
        {
            detachCri.addOrder(Order.desc("adddate"));
        }
        return detachCri;
    }

    private List findByCategoryType(String catetory, String type, int max)
    {
        DetachedCriteria detachCri = DetachedCriteria.forClass(Message.class);
        if (catetory != null && !"".equals(catetory))
        {
            detachCri.add(Restrictions.eq("category", catetory));
        }
        if (type != null && !"".equals(type))
        {
            detachCri.add(Restrictions.eq("type", type));
        }
        detachCri.addOrder(Order.desc("adddate"));
        return getMessageDAO().findByDetchedCriteria(detachCri, 0, max);
    }

    public List findBySearchForm(MsgSearchForm msf, int start, int max)
    {
        // TODO Auto-generated method stub
        DetachedCriteria detachCri = setDetachCri(msf);
        return getMessageDAO().findByDetchedCriteria(detachCri, start, max);
    }

    public int getCountBySearchForm(MsgSearchForm msf)
    {
        // TODO Auto-generated method stub
        DetachedCriteria detachCri = setDetachCri(msf);
        return getMessageDAO().getCountByDetachedCriteria(detachCri);
    }

    public List findKnow(int max)
    {
        // TODO Auto-generated method stub
        return findByCategoryType(null, "know", max);
    }

    public List findNews(int max)
    {
        // TODO Auto-generated method stub
        return findByCategoryType(null, "news", max);
    }

    public List findKnowByCategory(String category, int max)
    {
        // TODO Auto-generated method stub
        return findByCategoryType(category, "know", max);
    }

    public List findNewsByCategory(String category, int max)
    {
        // TODO Auto-generated method stub
        return findByCategoryType(category, "news", max);
    }

    public Message findById(int id)
    {
        // TODO Auto-generated method stub
        return getMessageDAO().findById(id);
    }

}
